package view.mainscreen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JPanel;
import controller.Controller;
import model.mapping.PlayableMapping;
import model.visibility.PlayerView;
import model.visibility.TileBuilder;
import view.Viewport;
import view.map.BuildableHexTile;
import view.map.HexTileMapper;

public class AreaViewport extends Viewport
{ final static Dimension size = new Dimension(824,670);
  HexTileMapper AVmapper;
  JPanel AVPanel;
  Point AVpoint;
  PlayerView playerView;
  PlayableMapping mapping = PlayableMapping.getPlayableMapping();
  Controller controller;
  BuildableHexTile tiles [][];
  int cursor[] = new int[2];
  int xPad = 575;
  int yPad = 235;
  public AreaViewport(PlayerView _playerView, Controller controller)
  { super(size);
  this.controller = controller;
    playerView = _playerView;
    cursor[0]= 0;
    cursor[1]= 0;
    AVmapper = new HexTileMapper( playerView.getMapLength(), playerView.getMapHeight() );
    AVPanel = new JPanel(AVmapper);
    AVPanel.setSize(size);
    AVPanel.setBackground(Color.black);
    //AVpoint = new Point(0,0);
    tiles = new BuildableHexTile[playerView.getMapHeight()][playerView.getMapLength()];
    for(int i = 0; i < playerView.getMapHeight();i++){
    	for(int j = 0; j < playerView.getMapLength();j++){
    		BuildableHexTile tile = new BuildableHexTile(i,j);
    		playerView.giveInitialTileInfo(tile);
    		playerView.build(tile);
    		AVPanel.add(tile);
    		tiles[i][j] = tile;
    	}
    }
    this.setView(AVPanel);
    this.setVisible(true);
  }
  public void moveToFocus(){
	  int x,y;
	  x = controller.getCurrentPlayable().getLocation().getX();
	  y = controller.getCurrentPlayable().getLocation().getY();
	  this.setViewPosition(validateMove(new Point(x*135 - xPad,y*117 - yPad)));
	  tiles[cursor[0]][cursor[1]].isCursor(false);
	  cursor[0] = x;
	  cursor[1] = y;
	  tiles[x][y].isCursor(true);
	  
  }
  public void moveCursorNorth(){
	  //135,117
	  /*
	  Point view = this.getViewPosition();
	  view = validateMove(new Point(view.x,view.y-117));
	  this.setViewPosition(view);
	  */

	  tiles[cursor[0]][cursor[1]].isCursor(false);
		cursor[1]--;
		validateCursor();
		tiles[cursor[0]][cursor[1]].isCursor(true);
		this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  public void moveCursorNorthEast(){
	//135,117
	  /*
	  Point view = this.getViewPosition();
	  view = validateMove(new Point(view.x + 100,view.y-58));
	  this.setViewPosition(view);
	  */
	  tiles[cursor[0]][cursor[1]].isCursor(false);
	  if(cursor[0] % 2 == 0){
		  cursor[0]++;
		  cursor[1]--;
	  }
	  else{
		  cursor[0]++;
	  }
	  validateCursor();
	  tiles[cursor[0]][cursor[1]].isCursor(true);
	  this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  public void moveCursorNorthWest(){
	//135,117
	  /*
	  Point view = this.getViewPosition();
	  view = validateMove(new Point(view.x -100,view.y-58));
	  this.setViewPosition(view);
	  */
	  tiles[cursor[0]][cursor[1]].isCursor(false);
	  if(cursor[0] % 2 == 0){
		  cursor[0]--;
		  cursor[1]--;
	  }
	  else{
		  cursor[0]--;
	  }
	  validateCursor();
	  tiles[cursor[0]][cursor[1]].isCursor(true);
	  this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  public void moveCursorSouth(){
	//135,117
	  /*
	Point view = this.getViewPosition();  
	view = validateMove(new Point(view.x,view.y+117));
	this.setViewPosition(view);
	*/
	tiles[cursor[0]][cursor[1]].isCursor(false);
	cursor[1]++;
	validateCursor();
	tiles[cursor[0]][cursor[1]].isCursor(true);
	this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  public void moveCursorSouthEast(){
	//135,117
	  /*
	  Point view = this.getViewPosition();
	  view = validateMove(new Point(view.x + 100,view.y+58));
	  this.setViewPosition(view);
	  */
	  tiles[cursor[0]][cursor[1]].isCursor(false);
	  if(cursor[0] % 2 == 0){
		  cursor[0]++;
	  }
	  else{
		  cursor[0]++;
		  cursor[1]++;
	  }
	  validateCursor();
	  tiles[cursor[0]][cursor[1]].isCursor(true);
	  this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  public void moveCursorSouthWest(){
	//135,117
	  /*
	  Point view = this.getViewPosition();
	  view = validateMove(new Point(view.x - 100,view.y+58));
	  this.setViewPosition(view);
	  */
	  tiles[cursor[0]][cursor[1]].isCursor(false);
	  if(cursor[0] % 2 == 0){
		  cursor[0]--;
	  }
	  else{
		  cursor[0]--;
		  cursor[1]++;
	  }
	  validateCursor();
	  tiles[cursor[0]][cursor[1]].isCursor(true);
	  this.setViewPosition(validateMove(new Point(cursor[0]*135 - xPad,cursor[1]*117 - yPad)));
  }
  private Point validateMove(Point view){
	  Dimension dim = this.getViewSize();
	  Dimension areaSize = this.getExtentSize();
	  Point max = new Point(dim.width - areaSize.width,dim.height - areaSize.height);
	  if(view.x < 0 && view.y < 0){
		  view = new Point(0,0);
	  }
	  else if(view.x < 0 && view.y > max.y){
		  view = new Point(0,max.y);
	  }
	  else if(view.x > max.x && view.y < 0){
		  view = new Point(max.x,0);
	  }
	  else if(view.x > max.x && view.y > max.y){
		  view = new Point(max.x,max.y);
	  }
	  else if(view.x < 0){
		  view = new Point(0,view.y);
	  }
	  else if(view.y < 0){
		  view = new Point(view.x,0);
	  }
	  else if(view.x > max.x){
		  view = new Point(max.x,view.y);
	  }
	  else if(view.y > max.y){
		  view = new Point(view.x,max.y);
	  }
	  return view;
  }
 public void validateCursor(){
	 if(cursor[0]< 0){
		 cursor[0] = 0;
	 }
	 if(cursor[1] < 0){
		 cursor[1] = 0;
	 }
	 if(cursor[0] >= tiles.length){
		 cursor[0] = tiles.length - 1;
	 }
	 if(cursor[1] >= tiles[cursor[0]].length){
		 cursor[1] = tiles[cursor[0]].length - 1;
	 }
 }
public void refreshAreaViewport() {
	int compCount = AVPanel.getComponentCount();
	for(int i = 0; i < compCount;i++){
		playerView.build((TileBuilder) AVPanel.getComponent(i));
	}
	//AVPanel.repaint();
}
}